/**
 * 初始化系统参数详情对话框
 */
var DoorInfoInfoDlg_ruleSelect = {
    doorInfoInfoData : {},
    userTree: null,
    selectedTree: null,
    deptTree: null,
    pre_ids: [],
    ids: []
};

/**
 * 清除数据
 */
DoorInfoInfoDlg_ruleSelect.clearData = function() {
    this.doorInfoInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
DoorInfoInfoDlg_ruleSelect.set = function(key, val) {
    this.doorInfoInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
DoorInfoInfoDlg_ruleSelect.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
DoorInfoInfoDlg_ruleSelect.close = function() {
    parent.layer.close(window.parent.DoorInfo.layerIndex);
}

/**
 * 验证数据是否为空
 */
DoorInfoInfoDlg_ruleSelect.validate = function () {
    $('#doorInfoForm').data("bootstrapValidator").resetForm();
    $('#doorInfoForm').bootstrapValidator('validate');
    return $("#doorInfoForm").data('bootstrapValidator').isValid();
};
/**
 * 提交添加
 */
DoorInfoInfoDlg_ruleSelect.submit = function() {

    this.clearData();

    //提交
    // 删除''的元素
    if(DoorInfoInfoDlg_ruleSelect.ids[0]==''){
        DoorInfoInfoDlg_ruleSelect.ids.shift();
        DoorInfoInfoDlg_ruleSelect.pre_ids.shift();
    }
    if(DoorInfoInfoDlg_ruleSelect.pre_ids.sort().toString() == DoorInfoInfoDlg_ruleSelect.ids.sort().toString()){
        Feng.error("未设置心得门禁权限！");
        return false;
    };

    var idsArr = "";
    for (var i = 0; i<DoorInfoInfoDlg_ruleSelect.ids.length; i++) {
        idsArr = (idsArr + DoorInfoInfoDlg_ruleSelect.ids[i]) + (((i + 1)== DoorInfoInfoDlg_ruleSelect.ids.length) ? '':',');
    }
    $('#idsArr').val(idsArr);
    this.doorInfoInfoData["idsArr"] = idsArr;
    this.doorInfoInfoData["id"] = $("[id='id']").val();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/door/DOOR/INFO/assignRule", function(data){
        Feng.success("设置成功!");
        DoorInfoInfoDlg_ruleSelect.close();
    },function(data){
        Feng.error("设置失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.doorInfoInfoData);
    ajax.start();
}
var setting = {view: {selectedMulti:false,nameIsHTML:true,showTitle:false,dblClickExpand:false},
    data: {simpleData: {enable: true}},
    callback: {onClick: treeOnClick}};

//点击选择项回调
function treeOnClick(event, treeId, treeNode, clickFlag){
    $.fn.zTree.getZTreeObj(treeId).expandNode(treeNode);
    if("ruleTree"==treeId){
        if($.inArray(String(treeNode.id), DoorInfoInfoDlg_ruleSelect.ids)<0){
            DoorInfoInfoDlg_ruleSelect.selectedTree.addNodes(null, treeNode);
            DoorInfoInfoDlg_ruleSelect.ids.push(String(treeNode.id));
        }
    };
    if("selectedTree"==treeId){
        DoorInfoInfoDlg_ruleSelect.selectedTree.removeNode(treeNode);
        DoorInfoInfoDlg_ruleSelect.ids.splice($.inArray(String(treeNode.id), DoorInfoInfoDlg_ruleSelect.ids), 1);
    }
};

$(function() {
    Feng.initValidator("doorInfoForm", DoorInfoInfoDlg_ruleSelect.validateFields);

    DoorInfoInfoDlg_ruleSelect.pre_ids= [];
    DoorInfoInfoDlg_ruleSelect.ids= [];
    //初始化已添加权限树
    $.get(Feng.ctxPath + "/door/DOOR/INFO/selectRulesForDoor?id=" + $("[id='id']").val(), function(rules){
        for (var i = 0; i < rules.length; i++) {
            rules[i].name = "<font color='red' style='font-weight:bold;'>" + rules[i].id + "-" + rules[i].ruleName + "</font>";
            DoorInfoInfoDlg_ruleSelect.pre_ids.push(rules[i].id)
            DoorInfoInfoDlg_ruleSelect.ids.push(rules[i].id)
        }
        DoorInfoInfoDlg_ruleSelect.selectedTree = $.fn.zTree.init($("#selectedTree"), setting, rules);
    });

    //初始化所有权限树
    $.get(Feng.ctxPath + "/door/ENTRANCE/GUARD/RULE/getRuleList?id=" + $("[id='id']").val(), function(rules){
        for (var i = 0; i < rules.length; i++) {
            rules[i].name = rules[i].id + "-" + rules[i].ruleName;
        }
        DoorInfoInfoDlg_ruleSelect.ruleTree = $.fn.zTree.init($("#ruleTree"), setting, rules);
    });
});
